﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain.Commands.CommandParameters;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;
using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.Common.Locator;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsPageSaveDataCommand : CmsPageSaveCommand
    {
        public override IQueryable<CmsPage> Execute()
        {
            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsPage>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsPage>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsPage, int>>();

                var newId = SqlUtility<CmsPage, int>.ExecuteNonQuery(
                            currentUnitOfWork.Connection,
                            currentUnitOfWork.Transaction,
                            "cms_sp_PageSave",
                            CommandParameter.Page,
                            mapper,
                            true);

                CommandParameter.Page.Id = newId;
            }

            return null;
        }

    }
}
